package com.tansun.easycare.rule.table.evaluationindex.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.JsonMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.table.decisiontable.service.DecisionTableService;
import com.tansun.easycare.rule.table.evaluationindex.model.IndexEntity;
import com.tansun.easycare.rule.table.evaluationindex.service.EvaluationIndexService;
import com.tansun.easycare.rule.utils.JsonUtil;
import com.tansun.rule.common.RuleModelTypeEnum;
import com.tansun.rule.utils.StringUtils;

@Controller
@RequestMapping(value = "/ruleProj/evaluationIndex")
public class EvaluationIndexController extends RuleBaseController {

    @Autowired
    private RuleManageService ruleManageService;


    @Autowired
    private EvaluationIndexService evaluationIndexService;

    @Autowired
    private DecisionTableService decisionTableService;

    /**
     * 赋值索引模型列表
     *
     * @param projectCode 项目code
     * @param param       赋值索引模型
     * @return 赋值索引路径
     */
    @RequestMapping("index")
    public String index(HttpServletRequest request, HttpServletResponse response, RuleManageVO param, ModelMap map) throws Exception {
        param.setProjectCode(getCurrProjectCode());
        param.setModelType(RuleModelTypeEnum.EvaluationIndex.getCode());
        PageInfo<RuleManageVO> page = this.ruleManageService.findRuleManageVO(param.getPage(), param);
        map.put("projectCode", this.getCurrProjectCode());
        map.put("page", page);
        map.put("vo", JsonUtil.object2json(param));
        return "rule/table/evaluationindex/index";
    }


    /**
     * 新增评分模型OR评分模型编辑
     *
     * @param indexId 赋值索引id
     * @param isedit  是否可编辑
     * @return 赋值索引详情页面
     */
    @RequestMapping(value = "detail")
    public String detail(HttpServletRequest request, String indexId, String isedit, ModelMap map) throws Exception {
        RuleManage rulemanage = new RuleManage();
        if (!StringUtils.isEmpty(indexId)) {
            rulemanage = this.ruleManageService.getById(indexId);
            if(rulemanage==null){
            	rulemanage = ruleManageService.findByCode(indexId, this.getCurrProjectCode());
            }
        }
        map.put("projectCode", this.getCurrProjectCode());
        map.put("indexModel", rulemanage);
        map.put("isedit", isedit);
        return "rule/table/evaluationindex/evaluationDetail";
    }

    /**
     * 保存或更新评分模型
     *
     * @param indexEntity 赋值索引实体
     * @return 是否保存成功对象
     */
    @RequestMapping("saveOrUpdate")
    public @ResponseBody
    JsonMessage saveOrUpdate(@RequestBody IndexEntity indexEntity) throws Exception {
        JsonMessage ret = new JsonMessage();
        try {
            evaluationIndexService.saveOrUpdate(indexEntity);
        } catch (Exception e) {
            logger.debug(ExceptionUtils.getStackTrace(e));

            ret.setMessage(e.getMessage());
            ret.setResult(false);
        }
        return ret;
    }


    /**
     * 获取赋值索引模型详情
     *
     * @param indexId 赋值索引id
     * @param version 版本号
     * @return 赋值索引详情，包含模型信息和规则信息
     */
    @RequestMapping(value = "getIndexDetail")
    public @ResponseBody
    JsonMessage getIndexDetail(HttpServletRequest request, String indexId, @RequestParam(value = "version", required = false) String version) throws Exception {
        JsonMessage ret = new JsonMessage();
        try {
            RuleManage indexModel = this.ruleManageService.getById(indexId);
            IndexEntity entity = new IndexEntity();
            entity.setIndexModel(indexModel);
            if (StringUtils.isEmpty(version)) version = indexModel.getVersion();
            entity.setIndexInfoList(this.evaluationIndexService.findEvaluationIndexItemByIndexId(indexId, version));
            ret.setData(entity);
        } catch (Exception e) {
            logger.debug(ExceptionUtils.getStackTrace(e));
            ret.setResult(false);
            ret.setMessage("赋值索引信息获取失败");
        }
        return ret;
    }
}
